//
//  ViewController.m
//  09-UITableViewEding
//
//  Created by dllo on 16/7/26.
//  Copyright © 2016年 YMJ. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) NSMutableArray *nameArray;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong ) NSMutableArray <NSIndexPath *> *selectIndexArray;
@property (nonatomic, strong ) NSMutableArray <NSString *> *selectNameArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationController.navigationBar.translucent = NO;
    NSArray *arr = @[@"唐益梦", @"张合尉", @"高雅馨", @"符淋淋", @"孙遥", @"李毅然", @"于瀚", @"沙长健", @"卢思彤", @"徐昊", @"曹冠雄", @"陈丹明", @"罗浩", @"肖扬", @"王道林", @"宫雪", @"李成", @"冀永金", @"姜浩", @"罗鹏飞", @"李昊林", @"郭宝", @"马峰", @"刘坦奇", @"殷明静", @"邓凯", @"徐康", @"王鍠", @"韩旭", @"杨睿", @"张信宝", @"姜程程", @"包铭礼", @"杜江", @"苗钧豪"];
    self.nameArray = [NSMutableArray arrayWithArray:arr];
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"1"];
    _tableView.contentInset = UIEdgeInsetsMake(0, 0, 64, 0);
    // 初始化属性数组们
    _selectIndexArray = [NSMutableArray array];
    _selectNameArray = [NSMutableArray array];
    // 编辑按钮
    // 系统自带编辑属性(右边barButton)<编辑删除>
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    // 拦截 系统默认编辑方法
    NSLog(@"%@",NSStringFromSelector(self.editButtonItem.action));
    // 重写rightBarButton的点击方法
    self.editButtonItem.action = @selector(editAction);
#pragma mark - 两种方法
#pragma mark - 方法一:在tableView的编辑状态UIViewController的类目方法.能在编辑状态改变时触发此方法
#pragma mark - 方法二:获取rightBarButton的点击方法,此时点击方法为默认系统方法,所以通过属性访问到此方法,把该方法名字获取到,然后重写方法,在重写方法里面触发系统方法同时,重写(点击done后)要执行的操作:多选:名字数组添加内容,取消多选:名字数组移除内容- 在didselect 和didDeSelect
}
#pragma mark - 方法二
- (void)editAction {
//    [self respondsToSelector:<#(SEL)#>]
    [self performSelector:NSSelectorFromString(@"_toggleEditing:")];
    if (self.editing) {
        
    } else {
        [_nameArray removeObjectsInArray:_selectNameArray];
        [_tableView deleteRowsAtIndexPaths:_selectIndexArray withRowAnimation:UITableViewRowAnimationRight];
        // 清空之前的数组们
        [_selectNameArray removeAllObjects];
        [_selectIndexArray removeAllObjects];

    }
}
#pragma mark - tableViewDelegate
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 选择cell 的编辑样式(默认是delete)
    // 支持多选
    return UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.editing) {
        // 选中(多选) 添加到数组里面
        // 取出对应下标的名字
        // 取出对应下标
        [_selectNameArray addObject:_nameArray[indexPath.row]];
        [_selectIndexArray addObject:indexPath];
    }
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.editing) {
        // 再选中(取消) 移除数组
        [_selectNameArray removeObject:_nameArray[indexPath.row]];
        [_selectIndexArray removeObject:indexPath];
    }
}
#pragma mark - tableViewDataSource
// 打开tableView 编辑状态的方法(它是uiviewController的类目的方法)
#pragma mark - 方法一
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];
    if (editing) {
        NSLog(@"编辑");
    } else {
        // 删除数据源
        // 删除cell
        /**[_nameArray removeObjectsInArray:_selectNameArray];
        [_tableView deleteRowsAtIndexPaths:_selectIndexArray withRowAnimation:UITableViewRowAnimationRight];
        // 清空之前的数组们
        [_selectNameArray removeAllObjects];
        [_selectIndexArray removeAllObjects];*/
        NSLog(@"完成");
    }
}
// 确定是否开启cell的编辑状态
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;// 默认可以移动
}

// 提交样式(对应按钮的点击方法)同时<滑动删除>只和这个方法有关系
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleInsert) {
        NSLog(@"添加");
        [_nameArray insertObject:@"黄瓜" atIndex:indexPath.row];
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
    } else if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSLog(@"删除");
        // 对tableView进行添加删除需要先修改数据源(nsmuArray),再进行....编辑
        [_nameArray removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        // 无论删除还是添加都已经进行了刷新操作(自带reloadData)
        

    }
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
   // 更改数据源
    [_nameArray exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _nameArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"1" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor brownColor];
    cell.textLabel.text = _nameArray[indexPath.row];
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
